package nuist.hanniko.stu.webbackend.dao.mapper;

import nuist.hanniko.stu.webbackend.model.LwM2m.LwM2mDevice;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;

@Component
public interface LwM2mDeviceMapper {

    @Insert("insert into device(device_id, name, reg_time, up_time, state, type)" +
            "values (#{deviceId}, #{name}, #{regTime}, #{upTime}, #{state}, #{type})")
    void insert(LwM2mDevice device);

    @Select("select * from device")
    @Results({
            @Result(property = "deviceId", column = "device_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "regTime", column = "reg_time"),
            @Result(property = "upTime", column = "up_time"),
            @Result(property = "state", column = "state"),
            @Result(property = "type", column = "type")
    })
    List<LwM2mDevice> getAllDevice();

    @Select("select * from device limit #{start}, #{length}")
    @Results({
            @Result(property = "deviceId", column = "device_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "regTime", column = "reg_time"),
            @Result(property = "upTime", column = "up_time"),
            @Result(property = "state", column = "state"),
            @Result(property = "type", column = "type")
    })
    List<LwM2mDevice> getDeviceByIndex(@Param("start") int start,
                                       @Param("length") int length);

    @Select("select * from device where device_id = #{deviceId}")
    @Results({
            @Result(property = "deviceId", column = "device_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "regTime", column = "reg_time"),
            @Result(property = "upTime", column = "up_time"),
            @Result(property = "state", column = "state"),
            @Result(property = "type", column = "type")
    })
    LwM2mDevice getByDeviceId(String deviceId);

    @Select("select * from device where type = #{type}")
    @Results({
            @Result(property = "deviceId", column = "device_id"),
            @Result(property = "name", column = "name"),
            @Result(property = "regTime", column = "reg_time"),
            @Result(property = "upTime", column = "up_time"),
            @Result(property = "state", column = "state"),
            @Result(property = "type", column = "type")
    })
    List<LwM2mDevice> getByType(String type);

    @Update("update device set name=#{name}, up_time=#{upTime}, state=#{state}, type=#{type}")
    void update(LwM2mDevice device);

    @Update("update device set state = #{state} where device_id = #{deviceId}")
    void updateStateById(@Param("deviceId") String deviceId,
                         @Param("state") String state);

    @Update("update device set up_time = #{upTime} where device_id = #{deviceId}")
    void updateRegistration(@Param("deviceId") String deviceId,
                            @Param("upTime") Date upTime);

    @Delete("delete from device where device_id = #{deviceId}")
    void deleteById(String deviceId);
}
